package com.example.demo.qy.controller;

import com.example.demo.common.utils.PageUtil;
import com.example.demo.common.utils.ResultUtil;
import com.example.demo.common.vo.PageVo;
import com.example.demo.common.vo.Result;
import com.example.demo.qy.model.User;
import com.example.demo.qy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(value = "*")
@Api(value = "User")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "登录")
    public Object login(String username,String password){
        return userService.login(username,password);
    }

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "注册")
    public Result register(@ModelAttribute User user){
        return new ResultUtil<>().setData(userService.register(user));
    }

    @RequestMapping(value = "/refreshToken",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "refreshToken")
    public Result refreshToken(@RequestHeader String authorization){
        return new ResultUtil<>().setData(userService.refreshToken(authorization));
    }

    @RequestMapping(value = "/User/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "通过id获取")
    public Result<User> get(@PathVariable String id){
        User entity = userService.get(id);
        return new ResultUtil<User>().setData(entity);
    }

    @RequestMapping(value = "/User/getAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部数据")
    public Result<List<User>> getAll(){
        List<User> list = userService.getAll();
        return new ResultUtil<List<User>>().setData(list);
    }

    @RequestMapping(value = "/User/getByPage",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页获取")
    public Result<Page<User>> getByPage(@ModelAttribute PageVo page){
        Page<User> list = userService.findAll(PageUtil.initPage(page));
        return new ResultUtil<Page<User>>().setData(list);
    }

    @RequestMapping(value = "/User/save",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存数据")
    public Result<User> save(@ModelAttribute User entity){
        User e = userService.save(entity);
        return new ResultUtil<User>().setData(e);
    }

    @RequestMapping(value = "/User/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "更新数据")
    public Result<User> update(@ModelAttribute User entity){
        User e = userService.update(entity);
        return new ResultUtil<User>().setData(e);
    }

    @RequestMapping(value = "/User/del",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除数据")
    public Result<Object> delAll(@ModelAttribute User entity){
        userService.delete(entity);
        return new ResultUtil<Object>().setSuccessMsg("删除数据成功");
    }

    @RequestMapping(value = "/User/delByIds",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "批量通过ids删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids){
        for(String id:ids){
            userService.delete(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量删除数据成功");
    }

}
